import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def login(self):
        self.browser.get("http://likeshop/admin/")
        sleep(1)
        self.browser.find_element(By.NAME,'account').send_keys('mahiru')
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        self.browser.find_element(By.ID,'login').click()
        print('登录成功')
        sleep(2)     

    def test1(self):
        self.browser.get("http://likeshop/admin/")
        sleep(1)
        self.browser.find_element(By.NAME,'account').send_keys('mahiru')
        self.browser.find_element(By.NAME,'password').send_keys('123456')
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
        self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        iframe1 = self.browser.find_element(By.XPATH,'/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        self.browser.find_element(By.ID,'keyword').send_keys('文具')
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        tr_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        is_not_bug = True
        if len(tr_list) == 0:
            print('没有数据')
        else:
            for tr in tr_list:
                td = tr.find_element(By.XPATH,'./td[2]/div')
                title = td.text
                if '文具' not in title:
                    is_not_bug = False
                    print('有bug') 
        if is_not_bug:
            print('没有bug')



    def test_add_shop1(self):
        # 发布商品测试
        # 点击商品按钮
        self.browser.find_element(By.XPATH, "//*[@id='LAY-system-side-menu']/li[1]/a/cite").click()
        sleep(2)
        # 定位 iframe 子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位发布商品按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        # 定位商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 定位商品名称输入框并输入商品名称
        self.browser.find_element(By.NAME, 'name').send_keys('测试商品')
        sleep(1)
        # 定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        # 定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(1)
        # 定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        # 定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(1)
        # 定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        # 定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(1)

        # 定位商品主图的添加图片按钮并点击
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a").click()
        sleep(1)
        # 测试上传图片
        self.test_upload_img('E:\\software-testing-experiment-2021\\1.PNG', '//*[@id="layui-layer-iframe2"]')
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 定位商品轮播图的添加图片按钮并点击
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[1]/div/div/div[6]/div/div/a").click()
        sleep(1)
        # 测试上传图片
        self.test_upload_img('E:\\software-testing-experiment-2021\\2.PNG', '//*[@id="layui-layer-iframe3"]')
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 切换到价格设置标签页
        self.browser.find_element(By.XPATH, "/html/body/div/ul/li[2]").click()
        sleep(2)
        # 定位市场价输入框并输入市场价
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input").send_keys('100')
        sleep(1)
        # 定位价格输入框并输入价格
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input").send_keys('90')
        sleep(1)
        # 定位库存输入框并输入库存
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input").send_keys('80')
        sleep(1)
        # 切换到销售设置标签页
        self.browser.find_element(By.XPATH, "/html/body/div/ul/li[4]").click()
        sleep(2)
        # 定位配送方式的快递发货并点击
        self.browser.find_element(By.XPATH, "/html/body/div/div/div[4]/div[6]/div/div[1]/span").click()
        sleep(1)
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        sleep(1)
        # 点击保存按钮
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)



    def test_sj_shop(self):
        # 测试上架商品
        # 点击商品按钮
        self.browser.find_element(By.XPATH, "//*[@id='LAY-system-side-menu']/li[1]/a/cite").click()
        sleep(2)
        # 定位 iframe 子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位仓库中商品切换标签页
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        # 接收商品列表
        goods_list = self.browser.find_elements(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        sleep(2)
        if len(goods_list) > 0:
            # 定位第一个商品的上架按钮并点击
            goods_list[0].find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            # 定位确认上架按钮并点击
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')

    def test_upload_img(self,img_path, xpath_path):
        # 测试上传商品图片
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 定位图片上传子框架并进入子框架
        iframe_img = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(iframe_img)
        sleep(2)
        # 定位图片上传输入框并上传图片
        sleep(4)
        # 选择第一张图片
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(2)
        #点击使用图片按钮
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
        # 测试上传图片
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[1]/div/input').send_keys(img_path)

 


if __name__ == '__main__':
    lk = LikeShopTest()
    lk.login()
    lk.test_add_shop1()
    lk.test_sj_shop()